var express = require('express');
var router = express.Router();
var multiparty = require('multiparty');
var { usersModel, rolesModel, menuModel } = require('../../database/user');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

// 获取用户列表
router.get('/users', async (req, res) => {
  const users = await usersModel.find({});
  res.json({
    code: 200,
    data: users,
    message: '获取用户成功'
  });
});


// 修改用户
router.post('/updateUser', async (req, res) => {
  const { id, name, roles, status } = req.body;
  const user = await usersModel.findByIdAndUpdate(id, { name, roles, status });
  res.json({
    code: 200,
    data: user,
    message: '修改用户成功'
  });
});

// 删除用户
router.post('/deleteUser', async (req, res) => {
  const { id } = req.body;
  const user = await usersModel.findByIdAndDelete(id);
  res.json({
    code: 200,
    data: user,
    message: '删除用户成功'
  });
});


// 获取角色列表
router.get('/roles', async (req, res) => {
  const roles = await rolesModel.find({});
  res.json({
    code: 200,
    data: roles,
    message: '获取角色成功'
  });
});

// 获取菜单列表
router.get('/menu', async (req, res) => {
  const menu = await menuModel.find({});
  res.json({
    code: 200,
    data: menu,
    message: '获取菜单成功'
  });
});

module.exports = router;